package model;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Creat with IntelliJ IDEA
 * Description:用于封装博客表的基本操作
 * User:WZW
 * Date:2022-08-06
 * Time:23:04
 */
public class BlogDAO {
    // 1. 往博客表里插入一个博客
    public void insert(Blog blog) throws SQLException {
        // JDBC 基本代码
        // 1 和数据库建立连接
        Connection connection = DBUtil.getConnection();
        // 2 构造SQL语句
        String sql = "insert into blog values(null, ?, ?, ?, now())";
        PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
        statement.setString(1,blog.getTitle());
        statement.setString(2, blog.getContent());
        statement.setInt(3, blog.getUserId());
        // 3 执行SQL语句
        statement.executeUpdate();
        // 4 关闭连接，释放资源
        DBUtil.close(connection,statement,null);
    }
    // 2. 能够获取到博客表中的所有博客信息
    public List<Blog> selectAll() {
        List<Blog> blogs = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog";
            statement = (PreparedStatement) connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 这里需要对内容进行截断
                String content = resultSet.getString("content");
                if (content.length() > 50) {
                    content = content.substring(0,50) + "...";
                }
                blog.setContent(content);
                blog.setUserId(resultSet.getShort("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                DBUtil.close(connection,statement,resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return blogs;
    }
    // 3. 能够根据博客id获取到指定的博客内容
    public Blog selectOne(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from blog where blogId = ?";
            statement = (PreparedStatement) connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            //此处使用主键作为查询条件，查询结果要么是1，要么是0
            if (resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getShort("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                DBUtil.close(connection,
                        statement,
                        resultSet);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    // 4. 从博客表中，根据博客id删除博客
    public void delete(int blogId) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "delete from blog where blogId = ?";
            statement = (PreparedStatement) connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                DBUtil.close(connection, statement, null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
